/**
 * Copyright (C) 2011, mordredd666 [_at_] gmail.com
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package de.mordred.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;

public class FButton extends JPanel
{
	private static final long serialVersionUID = 8057031894579093820L;

	Color beforeclick;

	private FButton()
	{
		setLayout(new BorderLayout());
	}

	public FButton(String text)
	{
		this();
		beforeclick = this.getBackground();

		this.setMinimumSize(new Dimension(400, 80));
		this.setSize(new Dimension(400, 80));
		this.setPreferredSize(new Dimension(400, 80));

		this.add(new JLabel(" "), BorderLayout.WEST);
		this.add(new JLabel("<html><center>" + text + "</center></html>"), BorderLayout.CENTER);

		this.setBorder(BorderFactory
				.createBevelBorder(BevelBorder.LOWERED, Color.blue, Color.BLACK));
	}

	public FButton(String text, byte[] bytearray)
	{
		this(text, new ImageIcon(bytearray));
	}

	public FButton(String text, final ImageIcon bild)
	{
		this();
		this.setMaximumSize(new Dimension(300, 100));

		ImageIcon icon = new ImageIcon(bild.getImage()
				.getScaledInstance(40, 40, Image.SCALE_SMOOTH));

		JLabel picturelabel = new JLabel(icon);

		this.add(picturelabel, BorderLayout.WEST);
		this.add(new JLabel("<html>" + text + "</html>"), BorderLayout.CENTER);

		picturelabel.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				final JPopupMenu jpop = getPopupWithPicture(bild);
				jpop.setLocation((int) e.getLocationOnScreen().getX() - 10, (int) e
						.getLocationOnScreen().getY() - 10);
				jpop.setVisible(true);
			}
		});

	}

	public void wrongAnswer()
	{
		this.setBackground(Color.red);
	}

	public void rightAnswer()
	{
		this.setBackground(Color.GREEN);
	}

	public void setNormal()
	{
		this.setBackground(beforeclick);
	}

	public void setSelected()
	{
		this.setBackground(new Color(0x00, 0x66, 0xFF));
	}

	/**
	 * Gibt ein Popupfenster mit dem Bild
	 * 
	 * @param pic
	 * @return
	 */
	public static JPopupMenu getPopupWithPicture(ImageIcon pic)
	{
		final JPopupMenu jpop = new JPopupMenu();
		JMenuItem menuitem = new JMenuItem(pic);
		menuitem.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				jpop.setVisible(false);

			}
		});
		jpop.add(menuitem);
		return jpop;
	}

}
